package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.mapper.BorrowerMapper;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.entity.BorrowerAttach;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalIntegralGradeVO;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerAuthVO;
import com.atguigu.srb.core.service.BorrowerAttachService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.util.JwtUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author Atguigu
 * @since 2024-04-15
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private BorrowerAttachService borrowerAttachService;
    @Resource
    private DictService dictService;

    @Override
    public void saveBorrowerInfo(BorrowerAuthVO borrowerAuthVO, String token) {
        Long userId = JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userId);
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerAuthVO, borrower);
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setName(userInfo.getName());
        borrower.setUserId(userId);
        borrower.setStatus(1);
        borrower.setMobile(userInfo.getMobile());
        this.save(borrower);
        BorrowerAttach borrowerAttach = new BorrowerAttach();
        borrowerAttach.setBorrowerId(userId);
        String idCard1 = borrowerAuthVO.getIdCard1();
        if (!StringUtils.isNullOrEmpty(idCard1)) {
            borrowerAttach.setImageName("身份证正面");
            borrowerAttach.setImageType("idCard1");
            borrowerAttach.setImageUrl(idCard1);
        }
        String idCard2 = borrowerAuthVO.getIdCard2();
        if (!StringUtils.isNullOrEmpty(idCard2)) {
            borrowerAttach.setImageName("身份证反面");
            borrowerAttach.setImageType("idCard2");
            borrowerAttach.setImageUrl(idCard2);
        }
        String house = borrowerAuthVO.getHouse();
        if (!StringUtils.isNullOrEmpty(house)) {
            borrowerAttach.setImageName("房产证");
            borrowerAttach.setImageType("house");
            borrowerAttach.setImageUrl(house);
        }
        String car = borrowerAuthVO.getCar();
        if (!StringUtils.isNullOrEmpty(car)) {
            borrowerAttach.setImageName("车");
            borrowerAttach.setImageType("car");
            borrowerAttach.setImageUrl(car);
        }

        borrowerAttachService.save(borrowerAttach);
    }

    @Override
    public void borrowerListByPage(Page<Borrower> page, String keyword) {
        if (StringUtils.isNullOrEmpty(keyword)) {
            this.page(page);
        } else {
            this.page(page, Wrappers.lambdaQuery(Borrower.class).like(Borrower::getName, keyword)
                    .or().like(Borrower::getMobile, keyword)
                    .or().like(Borrower::getIdCard, keyword));
        }
    }

    @Override
    public BorrowerApprovalVO approvalInfo(Long id) {
        Borrower borrower = this.getOne(Wrappers.lambdaQuery(Borrower.class).eq(Borrower::getId, id));
        Long userId = borrower.getUserId();
        BorrowerApprovalVO borrowerApprovalVO = new BorrowerApprovalVO();
        BeanUtils.copyProperties(borrower, borrowerApprovalVO);
        List<BorrowerAttach> borrowerAttachList = borrowerAttachService.list(Wrappers.lambdaQuery(BorrowerAttach.class)
                .eq(BorrowerAttach::getBorrowerId, userId));
        for (BorrowerAttach borrowerAttach : borrowerAttachList) {
            if ("idCard1".equals(borrowerAttach.getImageType())) {
                borrowerApprovalVO.setIdCard1(borrowerAttach.getImageUrl());
            }
            if ("idCard2".equals(borrowerAttach.getImageType())) {
                borrowerApprovalVO.setIdCard2(borrowerAttach.getImageUrl());
            }
            if ("car".equals(borrowerAttach.getImageType())) {
                borrowerApprovalVO.setCar(borrowerAttach.getImageUrl());
            }
            if ("house".equals(borrowerAttach.getImageType())) {
                borrowerApprovalVO.setHouse(borrowerAttach.getImageUrl());
            }
        }
        //SELECT t2.`name`
        //FROM dict t1
        //	LEFT JOIN dict	t2 ON t1.`id` = t2.`parent_id`
        //WHERE t1.dict_code = 'industry' AND t2.`value`= 5
        borrowerApprovalVO.setIndustry(dictService.getNameByDictCodeAndValue("industry", borrower.getIndustry()));
        borrowerApprovalVO.setIncome(dictService.getNameByDictCodeAndValue("income", borrower.getIncome()));
        borrowerApprovalVO.setReturnSource(dictService.getNameByDictCodeAndValue("returnSource", borrower.getReturnSource()));
        borrowerApprovalVO.setEducation(dictService.getNameByDictCodeAndValue("education", borrower.getEducation()));
        borrowerApprovalVO.setContactsRelation(dictService.getNameByDictCodeAndValue("relation", borrower.getContactsRelation()));

        return borrowerApprovalVO;

    }

    @Override
    public void approval(BorrowerApprovalIntegralGradeVO borrowerApprovalIntegralGradeVO) {

        Integer borrowerStatus = borrowerApprovalIntegralGradeVO.getStatus();
        Long borrowId = borrowerApprovalIntegralGradeVO.getBorrowerId();
        Borrower borrower = this.getOne(Wrappers.lambdaQuery(Borrower.class).eq(Borrower::getId, borrowId));
        borrower.setStatus(borrowerStatus);
        baseMapper.updateById(borrower);

        Long userId = borrower.getUserId();
        UserInfo userInfo = userInfoService.getById(userId);
        userInfo.setBorrowAuthStatus(borrowerStatus);
        if (borrowerStatus.intValue() == 2) {
            int integralNew = userInfo.getIntegral();
            Integer baseIntegral = borrowerApprovalIntegralGradeVO.getBaseIntegral();
            if(baseIntegral!=null && baseIntegral > 0){
                integralNew += baseIntegral;
            }
            if (borrowerApprovalIntegralGradeVO.getIsIdCardOk()) {
                integralNew += 30;
            }
            if (borrowerApprovalIntegralGradeVO.getIsCarOk()) {
                integralNew += 60;
            }
            if (borrowerApprovalIntegralGradeVO.getIsHouseOk()) {
                integralNew += 100;
            }
            userInfo.setIntegral(integralNew);
        }

        userInfoService.updateById(userInfo);
    }
}
